﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using TTSIDAL;
using TTSModel;

namespace TTSDAL
{
    public class UnitType : BaseRepository<UnitTypeInfo>, IUnitType
    {
        #region 静态变量定义
        private const string PARM_UNITTYPE_ID = "@ID";
        private const string PARM_UNITTYPE_TCODE = "@Tcode";
        private const string PARM_UNITTYPE_TCODENAME = "@TcodeName";
        private const string PARM_UNITTYPE_TCODEVERSION = "@TcodeVersion";
        #endregion

        #region 数据检索部分
        public IList<UnitTypeInfo> LoadEntities()
        {
            return ExcuteSqlToGetEntities("UnitType_select");
        }
        public IList<UnitTypeInfo> LoadEntities(string tcode)
        {
            SqlParameter parm = new SqlParameter(PARM_UNITTYPE_TCODE, SqlDbType.NChar, 8);
            parm.Value = tcode;
            return ExcuteSqlToGetEntities("UnitType_selectbyTcode", parm);
        }
        public IList<UnitTypeInfo> GetTwoNewRecord(string tcode)
        {
            SqlParameter parm = new SqlParameter(PARM_UNITTYPE_TCODE, SqlDbType.NChar, 8);
            parm.Value = tcode;
            return ExcuteSqlToGetEntities("GetVersionByTcode", parm);
        }
        public IList<UnitTypeInfo> GetRandomVersion(int versionNum,string tcode)
        {
            SqlParameter[] parm = GetParameters();
            parm[0].Value = versionNum;
            parm[1].Value = tcode;
            return ExcuteSqlToGetEntities("GetVersionByTcode", parm);
        }
        public IList<UnitTypeInfo> GetAllVersion(string tcode)
        {
            SqlParameter parm = new SqlParameter(PARM_UNITTYPE_TCODE, SqlDbType.NChar, 8);
            parm.Value = tcode;
            return ExcuteSqlToGetEntities("GetALLVersionByTcode", parm);
        }
        public UnitTypeInfo LoadEntity<T1>(T1 id)
        {
            SqlParameter parm = GetParameter();
            parm.Value = id;
            return ExcuteSqlToGetEntity("UnitType_selectbyID", parm);
        }

        public UnitTypeInfo UnitType_GetbyTcode(string tcode)
        {
            SqlParameter parm = new SqlParameter(PARM_UNITTYPE_TCODE, SqlDbType.NChar, 8);
            parm.Value = tcode;
            return ExcuteSqlToGetEntity("UnitType_selectbyTcode", parm);
        }
        #endregion

        #region 重写基类方法
        public override UnitTypeInfo GetEntityRecord(SqlDataReader rdr)
        {
            UnitTypeInfo entity = new UnitTypeInfo()
            {
                ID = rdr.GetInt32(0),
                Tcode = rdr.GetValue(1).ToString(),
                TcodeName = rdr.GetValue(2).ToString(),
                TcodeVersion = rdr.GetValue(3).ToString()
            };
            return entity;
        }

        public override UnitTypeInfo GetEntityRecord()
        {
            UnitTypeInfo entity = new UnitTypeInfo();
            return entity;
        }

        public override string GetInsertsp()
        {
            return "UnitType_Insert";
        }

        public override string GetUpdatesp()
        {
            return "UnitType_Update";
        }

        public override string GetDeletesp()
        {
            return "UnitType_Delete";
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter parm = new SqlParameter(PARM_UNITTYPE_ID, SqlDbType.NVarChar, 50);
            return parm;
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter(PARM_UNITTYPE_ID,SqlDbType.Int,4),
                new SqlParameter(PARM_UNITTYPE_TCODE,SqlDbType.NChar,8),
              
            };
            return parms;
        }

        public override void SetParameters(UnitTypeInfo Entity, params SqlParameter[] Parms)
        {
            Parms[0].Value = Entity.ID;
            Parms[1].Value = Entity.Tcode;
            Parms[2].Value = Entity.TcodeName;
            Parms[3].Value = Entity.TcodeVersion;
        }
        #endregion
    }
}
